package com.zhxymm.book.controllers;

import com.zhxymm.book.dao.SheepAccountRepository;
import com.zhxymm.book.entity.SheepAccount;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;


@Controller
public class SecurityController {

    @Autowired
    BCryptPasswordEncoder bCryptPasswordEncoder;

    @Autowired
    SheepAccountRepository sheepAccountRepository;

    @GetMapping("/register")
    public String register(Model model){
        SheepAccount sheepAccount = new SheepAccount();
        model.addAttribute("sheepAccount",sheepAccount);
        return "security/register";
    }

    @PostMapping("/register/save")
    public String saveUser(Model model,SheepAccount sheepAccount){
        sheepAccount.setPassword(bCryptPasswordEncoder.encode(sheepAccount.getPassword()));
        sheepAccountRepository.save(sheepAccount);
        return "redirect:/";
    }

}

